#include "Parser.h"
#include "PKB.h"
#include "QueryPreprocessor.h"
#include "DesignExtractor.h"
#include "StallOptimizer.h"
#include "StallCompute.h"
#include "SimpleSim.h"
#include "GenerateOptimizedSimple.h"
#include "QueryOptimizer.h"
#include "QueryEvaulator.h"
#include "ResultFormatter.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <stdio.h>

using namespace std;

void Step1(string input_source, int level_optimize){
	Parse(input_source);
	Extract();
	SimpleSimulator();
	cout << "number of stall is " << CalculateStall() << endl;
	cout << "--------------------------------------------------" << endl;
	TNode* new_root = SimpleStallOptimize(AST::get_p_tnode_AST_root(),level_optimize);
	GenerateOptimizedSimple(new_root,"optimal.txt");
}

void Step2(string output_source){
	Parse(output_source);
	Extract();
	SimpleSimulator();
	cout << "number of stall is " << CalculateStall() << endl;
}

int main(){
	Parse("test.txt");
	Extract();
	GenerateQueryTree("while w; call c; Select <w,c.procName> such that Parent*(w,c)",false);
	OptimizeQuery();
	ResultTable* p_result_table = NULL;
	bool b_flag = BQE(&p_result_table);
	list<string> answer;
	if( p_result_table == NULL )
	{
		if( b_flag )
			answer.push_back("true");
		else
			answer.push_back("false");
	}
	// case of select non BOOLEAN
	else if( b_flag )
	{
			Format(p_result_table,answer);
	}

	// Clean up
	optimize_stop = false;
	QueryVarTable::ClearQueryVarTable();
	QueryTree::ClearQueryTree();

	list<string>::iterator it = answer.begin();
	while(it != answer.end()){
		cout << *it << endl;
		it++;
	}

	getchar();
}


